package com.neuedu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.DongShu;
import com.neuedu.model.FangChan;
import com.neuedu.model.XiaoQu;
import com.neuedu.service.DongShuService;
import com.neuedu.service.FangChanService;
import com.neuedu.service.impl.DongShuServiceImpl;
import com.neuedu.service.impl.FangChanServiceImpl;
import com.neuedu.util.R;
import com.neuedu.vo.PageVO;

import java.util.List;

public class DongShuController {
    private DongShuService dongShuService=new DongShuServiceImpl();

    public R page(JSONObject jsonObject){
        Integer pageNo=jsonObject.getInteger("pageNo");
        Integer pageSize=jsonObject.getInteger("pageSize");
        //在某两个日期之间，like 一个给定的小区名称。
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");

        PageVO<DongShu> pageVO = dongShuService.selectOnePage(pageNo, pageSize, startTime, endTime, name);

        return R.ok().setData(pageVO);
    }

    public R add(JSONObject jsonObject){
        String suoshuxiaoqu = jsonObject.getString("suoshuxiaoqu");
        String dongshumingcheng = jsonObject.getString("dongshumingcheng");
        String zonghushu = jsonObject.getString("zonghushu");
        String miaoshu = jsonObject.getString("miaoshu");
        DongShu dongShu=new DongShu();
        dongShu.setSuoshuxiaoqu(suoshuxiaoqu);
        dongShu.setDongshumingcheng(dongshumingcheng);
        dongShu.setZonghushu(zonghushu);
        dongShu.setMiaoshu(miaoshu);

        //调用service，录入一个新的栋数
        int result = dongShuService.add(dongShu);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }

    }

    public R del(JSONObject jsonObject){
        JSONArray delId = jsonObject.getJSONArray("delId");
        Integer [] deleteId=new Integer[delId.size()];
        for (int i = 0; i < delId.size(); i++) {
            deleteId[i]=delId.getInteger(i);
        }

        int result = dongShuService.del(deleteId);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }

    }

    /**
     * 修改展示信息
     * @param jsonObject
     * @return
     */
    public R showDongShu(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        DongShu dongShu = dongShuService.findDongShuById(id);
        return R.ok().setData(dongShu);
    }

    public R edit(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        DongShu dongShu = dongShuService.findDongShuById(id);
        String suoshuxiaoqu = jsonObject.getString("suoshuxiaoqu");
        String dongshumingcheng = jsonObject.getString("dongshumingcheng");
        String zonghushu = jsonObject.getString("zonghushu");
        String miaoshu = jsonObject.getString("miaoshu");
        dongShu.setSuoshuxiaoqu(suoshuxiaoqu);
        dongShu.setDongshumingcheng(dongshumingcheng);
        dongShu.setZonghushu(zonghushu);
        dongShu.setMiaoshu(miaoshu);
        int result=dongShuService.editDongShuInfo(dongShu);
        if(result>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }

    public R findDongShuByXiaoQuName(JSONObject jsonObject){
        String xiaoQuName = jsonObject.getString("communityName");
        List<DongShu> dongShus = dongShuService.findDongShuByXiaoquName(xiaoQuName);

        return R.ok().setData(dongShus);
    }
}
